/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Logica;

/**
 *
 * @author Cami
 */
public class Monitor {
        
	int cantLectores;
	boolean estaEscribiendo;

	public synchronized void comienzoLectura(){
            while(estaEscribiendo){
            	try {
                    wait();
		} 
		catch (InterruptedException e) {}
		cantLectores++;			
            }		
	}
	public synchronized void terminoLectura(){
            cantLectores--;
            if(cantLectores == 0)
                notify();		
	}
	public synchronized void comienzoEscritura(){
		while(estaEscribiendo || cantLectores > 0){
                    try {
			wait();
                    } 
                    catch (InterruptedException e) {}
                    estaEscribiendo=true;		
		}			
	}
	public synchronized void terminoEscritura(){
		estaEscribiendo=false;
		notify();		
	}	

    
}
